package com.lijian.study.mybatisplus.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeGenerator {
    public static void main(String[] args) {

        String projectPath = System.getProperty("user.dir");
        // 1、创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2、全局配置
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");

        gc.setServiceName("%sService"); // 去掉Service接口的首字母I
        gc.setEntityName("%sDO");
        gc.setAuthor("lijian");
        gc.setOpen(false);
        gc.setFileOverride(true);
        autoGenerator.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setUrl("jdbc:mysql://192.168.94.72:3306/guigu-auth?serverTimezone=GMT%2B8&useSSL=false");
        dsConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dsConfig.setUsername("root");
        dsConfig.setPassword("123456");
        dsConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dsConfig);

        // 4、包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.lijian.study");
        packageConfig.setModuleName("mybatisplus"); // 模块名
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setMapper("mapper");
        autoGenerator.setPackageInfo(packageConfig);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude("sys_user", "sys_role", "sys_menu");

        strategy.setNaming(NamingStrategy.underline_to_camel);// 数据库表映射到实体的命名策略

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); // restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); // url中驼峰转连字符

        autoGenerator.setStrategy(strategy);

        // 6、执行
        autoGenerator.execute();

    }
}